package org.djw.pet.service.impl;

import org.djw.basic.service.impl.BaseServiceImpl;
import org.djw.pet.domain.Pet;
import org.djw.pet.domain.PetDetail;
import org.djw.pet.mapper.PetDetailMapper;
import org.djw.pet.mapper.PetMapper;
import org.djw.pet.service.IPetService;
import org.djw.user.domain.Logininfo;
import org.djw.user.domain.User;
import org.djw.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PetServiceImpl  extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void add(Pet pet) {
        //保存宠物基本信息,并返回自增id
        petMapper.save(pet);
        //生成宠物详情
        PetDetail detail = pet.getDetail();
        System.out.println(detail);
        if (detail != null){
            detail.setPet_id(pet.getId());
            System.out.println(detail.getPet_id());
            petDetailMapper.save(detail);
        }

    }

    @Override
    public void onSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("onSaleTime", new Date());
        petMapper.onSale(map);
    }

    @Override
    public void offSale(List<Long> ids) {
        Map<String,Object> map = new HashMap<>();
        map.put("ids", ids);
        map.put("offSaleTime", new Date());
        petMapper.offSale(map);
    }

    @Override
    public Pet loadByIdDetail(Long id) {
        return petMapper.loadByIdDetail(id);
    }

    @Override
    public void adopt(Long id, Logininfo logininfo) {
        Pet pet = petMapper.loadById(id);
        //下架宠物
        pet.setState(0);
        pet.setOffsaletime(new Date());
        //绑定领养人
        User user = userMapper.loadByLogininfoId(logininfo.getId());
        pet.setUser_id(user.getId());
        petMapper.update(pet);

        //@TODO 生成领养订单
        //@TODO 生成支付单并支付
    }
}
